package cn.tedu.net;


import sun.misc.IOUtils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

//jdk7的新特性：自动资源管理|catch 多个类型异常
public class Test3 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("Hello" );
        list.add("Learn");
        list.add("Hello" );
        list.add("Welcome");
        Set set = new HashSet();
        System.out.println(list);
        set.addAll(list);
        System.out.println(set);
        System.out.println(set.size());

//        method();
//        method2();//优化method（）
    }
    //优化后：把对象的声明放在try的小括号里，用完自动释放
    private static void method2() {
        try( InputStream  in = new BufferedInputStream(new FileInputStream("？？"));) {
            int data=in.read();
            System.out.println(data);
        }catch(NullPointerException | IOException f){
            f.printStackTrace();//开发调试阶段用
        }
    }
    //IO读取数据
    public static void method() {
        InputStream in=null;
        try {
            in = new BufferedInputStream(new FileInputStream("？？"));
            int data=in.read();
            System.out.println(data);
        }catch(NullPointerException | IOException f){
        f.printStackTrace();//开发调试阶段用
        }finally {
            if(in!=null) {//防止;NullPointerException --空指针异常
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    in=null;//就算有异常，GC也会回收对象
                }
            }
        }
    }
}
